package com.example.myapplication1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class DeleteActivity extends AppCompatActivity {
    private ImageView headPic;
    private EditText username;
    private EditText password;
    private Button delete;
    private Button ret;
    private MyDatabaseHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete);
        dbHelper = new MyDatabaseHelper(this, "Userinfo.db", null, 2);
        headPic = (ImageView) findViewById(R.id.head_pic);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        delete = (Button) findViewById(R.id.delete);
        ret = (Button) findViewById(R.id.ret);
        ret.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(DeleteActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String u = username.getText().toString();
                String p = password.getText().toString();
                int x =0;
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                Cursor cursor = db.query("userinfo", null, null, null, null, null, null);
                ContentValues values = new ContentValues();
                SharedPreferences.Editor editor = getSharedPreferences("userinfo",MODE_PRIVATE).edit();
                editor.putString("username",u);
                editor.putString("password",p);
                editor.commit();
                if (cursor.moveToFirst()) {

                    do {
                        String username = cursor.getString(cursor.
                                getColumnIndex("username"));
                        String password = cursor.getString(cursor.
                                getColumnIndex("password"));

                        if (u.equals(username)) {

                            if(p.equals(password)){
                                x = 1;
                                break;
                            }
                            else {
                                x=2;
                                break;
                            }

                        }


                    } while (cursor.moveToNext());

                }
                cursor.close();

                if(x==0){
                    Toast.makeText(DeleteActivity.this, "没有此用户，删除失败!", Toast.LENGTH_SHORT).show();

                }
               if(x==1){
                   db.delete("userinfo", "username = ?", new String[] { u });
                   Toast.makeText(DeleteActivity.this, "删除成功!", Toast.LENGTH_SHORT).show();
                   Intent intent = new Intent(DeleteActivity.this, LoginActivity.class);
                   startActivity(intent);
               }
               if(x==2){
                   Toast.makeText(DeleteActivity.this, "密码错误，删除失败!", Toast.LENGTH_SHORT).show();

               }


            }
        });
    }
}